﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

namespace Simulation.Framework
{
    /// <summary>
    /// 加密管理
    /// </summary>
    public class EncryptionManager : ManagerBehavior
    {
        #region override

        public override void Init() { }

        public override void Clear() { }

        public override void Restar() { }

        #endregion

        /// <summary>
        /// 秘钥
        /// 激活码
        /// </summary>
        /// <param name="secretKey"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool GetResult(string code,string secretKey)
        {
            string uniqueIdentifier = GetUniqueIdentifier();
            string decryptInfo = DecryptString(code, secretKey);
            bool result = decryptInfo == uniqueIdentifier;
            return result;
        }

        private string GetUniqueIdentifier()
        {
            return SystemInfo.deviceUniqueIdentifier;
        }

        public static string EncryptString(string data, string key)
        {
            if (!KeyLengthIsFit(key))
            {
                return "";
            }

            byte[] bytes = Encoding.UTF8.GetBytes(key);
            RijndaelManaged rm = new RijndaelManaged();
            rm.Key = bytes;
            rm.Mode = CipherMode.ECB;
            rm.Padding = PaddingMode.PKCS7;

            ICryptoTransform cryptoTransform = rm.CreateEncryptor();
            String encryptStr = string.Empty;

            try
            {
                byte[] decryptData = Encoding.UTF8.GetBytes(data);
                byte[] encryptData = cryptoTransform.TransformFinalBlock(decryptData, 0, decryptData.Length);
                encryptStr = Convert.ToBase64String(encryptData, 0, encryptData.Length);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }

            return encryptStr;
        }

        public static string DecryptString(string data, string key)
        {
            if (!KeyLengthIsFit(key))
            {
                return "";
            }

            byte[] bytes = Encoding.UTF8.GetBytes(key);
            RijndaelManaged rm = new RijndaelManaged();
            rm.Key = bytes;
            rm.Mode = CipherMode.ECB;
            rm.Padding = PaddingMode.PKCS7;

            ICryptoTransform cryptoTransform = rm.CreateDecryptor();
            String decryptStr = string.Empty;

            try
            {
                byte[] encryptBytes = Convert.FromBase64String(data);
                byte[] decryptBytes = cryptoTransform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length);
                decryptStr = Encoding.UTF8.GetString(decryptBytes);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }

            return decryptStr;
        }

        private static bool KeyLengthIsFit(string key)
        {
            int len = key.Length;
            if (len == 16 || len == 24 || len == 32){
                return true;
            }
            Debug.Log("请使用长度为16/24/32位的密钥！");
            return false;
        }
    }
}